package com.bytz.modules.cms.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bytz.common.system.query.MPJLambdaWrapperEx;
import com.bytz.common.system.query.MPJQueryGenerator;
import com.bytz.modules.cms.domain.manager.CmsManager;
import com.bytz.modules.cms.domain.model.price.PriceComputeResult;
import com.bytz.modules.cms.product.constant.ProductConstants;
import com.bytz.modules.cms.product.entity.Product;
import com.bytz.modules.cms.product.mapper.ProductMapper;
import com.bytz.modules.cms.product.model.ProductModel;
import com.bytz.modules.cms.product.service.IMallProductHierarchyService;
import com.bytz.modules.cms.product.service.IMallProductService;
import com.bytz.modules.cms.product.service.IProductService;
import com.bytz.modules.cms.reseller.service.IResellerLevelService;
import com.bytz.modules.cms.reseller.util.ResellerSecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class MallProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IMallProductService {

    @Autowired
    private IProductService productService;

    @Autowired
    private IResellerLevelService resellerLevelService;

    @Autowired
    private IMallProductHierarchyService productHierarchyService;

    @Autowired
    private CmsManager cmsManager;

    @Override
    public List<ProductModel> searchProduct(String productNumber) {

        return BeanUtil.copyToList(productService.searchProduct(productNumber), ProductModel.class);
    }

    @Override
    public List<ProductModel> getProductDetailByNumbers(Collection<String> productNumbers) {

        List<Product> products = productService.getByProductNumbers(productNumbers);

        return calcPrice(products);
    }

    @Override
    public List<ProductModel> getProductDetailByIds(Collection<String> ids) {

        List<Product> products = productService.getByIds(ids);

        return calcPrice(products);
    }

    @Override
    public IPage<ProductModel> queryPage(Page<Product> page, Product product, Map<String, String[]> parameterMap) throws Exception {

        String resellerId = ResellerSecurityUtils.getUserId();

//        Set<String> levelIds = resellerLevelService.getByResellerId(resellerId)
//                .stream().map(ResellerLevel::getId)
//                .collect(Collectors.toSet());

        MPJLambdaWrapperEx<Product> queryWrapper = MPJQueryGenerator.initQueryWrapper(product, parameterMap, Product.class);
        IPage<Product> productPage = baseMapper.selectJoinPage(page, Product.class, queryWrapper
                        .selectAll(Product.class)
//                .leftJoin(ResellerProduct.class, on -> on
//                        .eq(Product::getId, ResellerProduct::getProductId)
//                        .eq(ResellerProduct::getOffsetType, ResellerConstant.ResellerProductOffsetType.NO_PRICE)
//                        .eq(ResellerProduct::getDelFlag, CommonConstant.DEL_FLAG_0)
//                        .eq(ResellerProduct::getResellerId, resellerId)
//                )
//                .leftJoin(CollectionUtils.isNotEmpty(levelIds), ProductLineLevelLimit.class, on -> on
//                        .eq(Product::getId, ProductLineLevelLimit::getProductId)
//                        .eq(ProductLineLevelLimit::getDelFlag, CommonConstant.DEL_FLAG_0)
//                        .in(ProductLineLevelLimit::getProductLineLevelId, levelIds)
//                )
                        .eq(Product::getStatus, ProductConstants.ProductStatus.ENABLE)
//                .isNull(ResellerProduct::getId)
//                .isNull(ProductLineLevelLimit::getId)
        );

        List<ProductModel> productModels = calcPrice(productPage.getRecords());

        IPage<ProductModel> result = new Page<>();
        BeanUtils.copyProperties(productPage, result);

        result.setRecords(productModels);

        return result;
    }

    private List<ProductModel> calcPrice(List<Product> products) {

        String resellerId = ResellerSecurityUtils.getUserId();

        List<ProductModel> modelList = BeanUtil.copyToList(products, ProductModel.class);
        Map<String, PriceComputeResult> priceResult = cmsManager.getProductPriceManager().compute(resellerId, products);
        for (ProductModel productModel : modelList) {
            PriceComputeResult price = priceResult.get(productModel.getProductNumber());
            if (price != null) {
                productModel.setCalculatePrice(price.getPrice());
            }
        }

        productHierarchyService.setHierarchyLevelInfo(modelList);

        return modelList;
    }
}